package no.rodland.acc.parsers;

import org.apache.log4j.Logger;
import org.joda.time.DateTime;

import java.util.List;

import no.rodland.acc.model.Bank;
import no.rodland.acc.model.BankType;
import no.rodland.acc.db.StoreService;
import no.rodland.acc.db.FetchService;
import no.rodland.acc.utils.Config;

/**
 * Created by IntelliJ IDEA.
 *
 * @Since: Jun 21, 2010 10:44:31 AM
 * @Author: fmr
 */
public class SkandiaBankParser extends AbstractBankParser {

    private static final Logger log = Logger.getLogger(SkandiaBankParser.class);

    public SkandiaBankParser(FetchService fetchService,
                             StoreService storeService,
                             DateParser dateParser,
                             NumberParser numberParser,
                             TextDateParser textDateParser) {
        super(fetchService, storeService, dateParser, numberParser, textDateParser);
    }

    @Override
    public void configure() {
        this.headerRowRegexp = Config.getProperty("import.header_row_regexp");
        this.separator = Config.getProperty("import.separator");
    }



    @SuppressWarnings({"unchecked"})
    @Override
    public List<Bank> parseLine(String line) throws ParseException {
        try {
            String[] arr = line.split(separator);

            DateTime interestDate = dateParser.parse(arr[0]);
            DateTime accountDate = dateParser.parse(arr[1]);
            String ref = arr[2];
            String btString = arr[3];
            String text = arr[4];
            Integer amountOut = numberParser.parse(arr[5]);
            Integer amountIn = 0;
            if (arr.length == 7) {
                amountIn = numberParser.parse(arr[6]);
            }

            String budgeted = "N";
            String comment = "";

            DateTime useDate = textDateParser.parse(text, interestDate);
            BankType bankType = getBankType(btString);

            return makeList(new Bank(interestDate,
                    accountDate,
                    useDate,
                    ref,
                    bankType,
                    text,
                    amountOut,
                    amountIn,
                    bankAccount,
                    budgeted,
                    comment));
        }
        catch (Exception e) {
            log.error("Unable to parse line: " + line, e);
            throw new ParseException("Unable to parse line", line, e);
        }
    }

    @Override
    public String getName() {
        return "Skandiabanken";
    }
}
